package pub.wkq.api.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 的配置类（用于死信队列的配置）
 */
@Configuration
public class RabbitMQSMSDeadConfig {
    //定义交换机的名称
    public static final String SMS_EXCHANGE_DEAD_NAME = "sms_exchange_dead";
    //定义队列的名称
    public static final String SMS_QUEUE_DEAD_NAME = "sms_queue_dead";
    //定义routingKey
    public static final String SMS_DEAD_ROUTING_KEY = "dead.sms.#";
    public static final String SEND_SMS_ROUTING_KEY = "dead.sms.send.login";
    public static final String SEND_EMAIL_DEAD_ROUTING_KEY = "dead.sms.mail.send.login";

    /**
     * 创建交换机
     *
     * @return 交换机
     */
    @Bean(SMS_EXCHANGE_DEAD_NAME)
    public Exchange exchange() {
        return ExchangeBuilder.topicExchange(SMS_EXCHANGE_DEAD_NAME)
                .durable(true)
                .build();
    }

    /**
     * 创建队列
     *
     * @return 队列
     */
    @Bean(SMS_QUEUE_DEAD_NAME)
    public Queue queue() {
        return QueueBuilder.durable(SMS_QUEUE_DEAD_NAME)
                .build();
    }

    /**
     * 创建绑定关系
     *
     * @param queue    队列
     * @param exchange 交换机
     * @return 绑定关系
     */
    @Bean(SMS_QUEUE_DEAD_NAME+SMS_EXCHANGE_DEAD_NAME)
    public Binding binding(@Qualifier(SMS_QUEUE_DEAD_NAME) Queue queue,
                           @Qualifier(SMS_EXCHANGE_DEAD_NAME) Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(SMS_DEAD_ROUTING_KEY)
                .noargs();
    }
}
